const express = require('express')
const router =  express.Router()
const stydentgradesSchema = require('../../model/home/student')
const jwtmidd = require('../../midd/jwtMidd')
let result = {
    data: {},
    msg: '',
    code: 200
}
// 添加成绩单
router.post('/grades/add',jwtmidd,async (request,response)=>{
    // 测试数据
    // let datatest = {        userid: '0001',        grades: [            {                name: 'javaee',                count: 69            },            {                name: 'javaweb',                count: 62   },    {               name: 'spring',               count: 81           },           {               name: 'mybates',               count: 78           },           {               name: 'springmvc',               count: 92           }       ]}
    // console.log(JSON.stringify(datatest));
    console.log(request.body);
    const {userid,grades} = request.body
    const gradesData = JSON.parse(grades)
    let userInfo = await stydentgradesSchema.findOne({userid})
    if(userInfo){
        userInfo.grades.push(gradesData)
        stydentgradesSchema.updateOne({
            userid,grades:userInfo.grades
        }).then(res=>{
            console.log(res,'success');
            if(res){
                result.data = res
                result.msg = '添加成功'
                response.json(result)
                return
            }
            result.data = res
            result.msg = '添加失败'
            response.json(result)
        }).catch(err=>{
            console.log(err,'err');
            result.data = err
            result.msg = '添加失败'
            response.json(result)
        })
    }else{
        console.log(err,'err');
        result.msg = '用户不存在'
        response.json(result)
        return
    }
})
// 查询成绩单
router.get('/grades',jwtmidd,(request,response)=>{
    console.log(request.query);
    if(request.query.userid){
        stydentgradesSchema.findOne({userid:request.query.userid}).then(res=>{
            result.data = res
            if(res){
                result.msg = '查询成功'
            }else{
                result.msg = '没有该用户成绩单'
            }
             response.json(result)
        }).catch(err=>{
            result.data = err
            result.msg = '查询失败'
            response.json(result)
        })
    }
})

// 校验token状态是否有效
router.get('/token',jwtmidd,(request,response)=>{
    result.msg = '校验成功'
    response.json(result)
})

// 修改成绩单
router.post('/grades/update',jwtmidd,(request,response)=>{
    console.log(request.body,'cccccccccccccccccccccccccc');
    const {userid,grades} = request.body
    const gradesData = JSON.parse(grades)
    stydentgradesSchema.updateOne({userid},{grades: gradesData}).then(res=>{
        console.log(res);
        result.data = res
        if(res){
            result.msg = '修改成功'
            response.json(result)
        }else{
            result.msg = '用户不存在'
            response.json(result)
        }
    }).catch(err=>{
        console.log(err);
        result.msg = '修改失败'
        result.code = 2001
        response.json(result)
    })
})

// 删除成绩单
router.post('/grades/delete',async (request,response)=>{
    console.log(request.body);
    const {userid,subjectName} = request.body
    console.log(userid,subjectName);
     let userInfo =  await stydentgradesSchema.findOne({userid})
     userInfo.grades = userInfo.grades.filter(it=>{
        return it.name != subjectName
     })
     let resultData = await stydentgradesSchema.updateOne({userid},{grades: userInfo.grades})
    if(resultData){
        result.data = resultData
        response.json(result)
    }else{
        result.data = resultData
        result.code = 2001
        response.json(result)
    }
})
module.exports = router